//
//  UILabel+MyLabel.m
//  QuickCoin
//
//  Created by tjl on 16/11/22.
//  Copyright © 2016年 tjl. All rights reserved.
//

#import "UILabel+MyLabel.h"
#import "Config.h"

@implementation UILabel (MyLabel)

+ (UILabel *)createLabelWithFrame:(CGRect)frame title:(NSString *)title textColor:(id)textColor isCenter:(BOOL)center font:(float)font andHaveUnderLine:(BOOL )have board:(BOOL )board {
    
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    
    label.text = title;
    
    label.font = [UIFont fontWithName:board?kFontNameBold:kFontNameLight size:font];
    
    label.textColor = textColor?textColor:[UIColor whiteColor];
    
    label.textAlignment = center? NSTextAlignmentCenter:NSTextAlignmentLeft;
    
    if (have) {
        // 下划线
        UILabel *label1 = [UILabel createLineLabelWithFrame:CGRectMake(0, label.bounds.size.height-2, label.bounds.size.width, kLineWitdth)];
        /*设置label的frame值*/
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
        paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
        label.numberOfLines = 0;
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:title];;
        [paragraphStyle setLineSpacing:5];
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, title.length)];
        label.attributedText = attributedString;
        
        CGSize size = [label sizeThatFits:CGSizeMake(DeviceWidth, 20)];
        label1.frame = CGRectMake(-2, label.bounds.size.height-2, size.width+30, kLineWitdth);
        //赋值
        [label addSubview:label1];
    }
    label.adjustsFontSizeToFitWidth = YES;
    return label;
    
}

+ (UILabel *)createLineLabelWithFrame:(CGRect)frame  {
    
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    
    label.backgroundColor = kLineColor;
    
    return label;
}

+ (float)getTextWidthWithString:(NSString *)text font:(float)font {
    
    float width = [self getTextWidthWithString:text font:font bord:NO];
    return width;
}
+ (float)getTextWidthWithString:(NSString *)text font:(float)font bord:(BOOL)bord
{
    if (text == nil) {
        text = @"";
    }
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, DeviceWidth-30, 60)];
    
    label.lineBreakMode = NSLineBreakByWordWrapping;
    label.backgroundColor = [UIColor clearColor];
    /*设置label的frame值*/
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    label.numberOfLines = 0;
    label.font = [UIFont fontWithName:bord?kFontNameBold:kFontNameLight size:font?font:labelMiddleFont];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:text];;
    [paragraphStyle setLineSpacing:5];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, text.length)];
    
    label.attributedText = attributedString;
    //调节高度
    CGSize size = CGSizeMake(DeviceWidth-30, 20);
    
    CGSize labelSize = [label sizeThatFits:size];
    
    return labelSize.width+32;
}
+ (UILabel *)setIntroductionTextBody:(NSString*)text andFont:(float)font {
    return [self setIntroductionTextBody:text andFont:font frame:CGRectMake(15, 0, DeviceWidth-30, 20)];
}
+ (UILabel *)setIntroductionTextBody:(NSString*)text andFont:(float )font frame:(CGRect)frame {
	
	return [self setIntroductionTextBody:text andFont:font frame:frame textColor:kTextColor];
}

+ (UILabel *)setIntroductionTextBody:(NSString*)text andFont:(float )font frame:(CGRect)frame textColor:(id)textcolor {
    return [self setIntroductionTextBody:text andFont:font frame:frame textColor:textcolor board:NO];
}
+ (UILabel *)setIntroductionTextBody:(NSString *)text andFont:(float)font frame:(CGRect)frame textColor:(id)textcolor board:(BOOL)board
{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    
    label.lineBreakMode = NSLineBreakByWordWrapping;
    label.backgroundColor = [UIColor clearColor];
    /*设置label的frame值*/
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    label.numberOfLines = 0;
    label.font = [UIFont fontWithName:board?kFontNameBold:kFontNameLight size:font?font:labelMiddleFont];
    
    
    label.textColor = [UIColor whiteColor];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:text];;
    [paragraphStyle setLineSpacing:5];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, text.length)];
    [attributedString addAttribute:NSForegroundColorAttributeName value:textcolor range:NSMakeRange(0, text.length)];
    NSArray *array = [text componentsSeparatedByString:@"\n"];

    [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSArray *temp = [obj componentsSeparatedByString:@"："];
        if (temp && temp.count>1) {
            NSRange rang = NSMakeRange([text rangeOfString:obj].location, [temp[0] length]);
            [attributedString addAttribute:NSForegroundColorAttributeName value:kTextColor range:rang];
        }
        
    }];
    label.attributedText = attributedString;
    //调节高度
    CGSize size = CGSizeMake(frame.size.width, 500000);
    
    CGSize labelSize = [label sizeThatFits:size];
    
    label.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, labelSize.height);
    
    return label;
}

+ (UILabel *)createLineLabelWithframe:(CGRect)frame {
    
    UILabel *line = [[UILabel alloc] initWithFrame:frame];
    line.backgroundColor = kLineColor;
    return line;
    
}

+ (UILabel *)createLabelWithNormolText:(NSString *)text textColor:(id)textColor specialText:(NSString *)specialText specialColor:(id)specialColor font:(int)font isCenter:(BOOL)center frame:(CGRect )frame {
	
	UILabel *label = [UILabel createLabelWithFrame:frame title:text textColor:textColor isCenter:center font:font andHaveUnderLine:NO board:NO];
	NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:text];
	[str addAttribute:NSForegroundColorAttributeName value:textColor range:NSMakeRange(0, text.length)];
	[str addAttribute:NSForegroundColorAttributeName value:specialColor range:NSMakeRange([text rangeOfString:specialText].location, specialText.length)];
	[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:kFontNameLight size:font] range:NSMakeRange(0, text.length)];

	label.attributedText = str;
	
	return label;
	
}

- (void)refreshLabel:(UILabel *)label textColor:(id)normalColor specialText:(NSString *)specialText specialColor:(id)specialColor {
	
	if (![specialText length] || [specialText containsString:@"null"] || ![label.text containsString:specialText]) {
		return;
	}
	
	NSString *text = label.text;
	
	NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:text];
	[str addAttribute:NSForegroundColorAttributeName value:normalColor range:NSMakeRange(0, text.length)];
	[str addAttribute:NSForegroundColorAttributeName value:specialColor range:NSMakeRange([text rangeOfString:specialText].location, specialText.length)];
	label.attributedText = str;
	
}

- (void)refreshLabelFont:(UILabel *)label textFont:(int)normalFont specialText:(NSString *)specialText specialFont:(int)specialFont {
	
	if (![specialText length] || [specialText containsString:@"null"] || ![label.text containsString:specialText]) {
		return;
	}
	
	NSString *text = label.text;
	
	NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:text];
	[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:kFontNameLight size:normalFont] range:NSMakeRange(0, text.length)];
	[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:kFontNameLight size:specialFont] range:NSMakeRange([text rangeOfString:specialText].location, specialText.length)];
	label.attributedText = str;
	
}

@end
